package top.kaoshanji.p1ch06a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static top.kaoshanji.p1ch06a.Dish.menu;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/12 11:13
 */
public class Partitioning {

    private static final Logger logger = LogManager.getLogger(Partitioning.class);

    public static void main(String[] args) {

        // 代码清单-p1ch06a-135页a：把菜单按照素食和非素食分开
        logger.info("...把菜单按照素食和非素食分开...");
        System.out.println(partitionByVegeterian());


        // 代码清单-p1ch06a-135页b：把菜单按照素食分类后继续按类型分组
        logger.info("...把菜单按照素食分类后继续按类型分组...");
        System.out.println(vegetarianDishesByType());


        // 代码清单-p1ch06a-135页c：把菜单按照素食分类后筛选其中热量最高的菜
        logger.info("...把菜单按照素食分类后筛选其中热量最高的菜...");
        System.out.println(mostCaloricPartitionedByVegetarian());



    }


    private static Map<Boolean, List<Dish>> partitionByVegeterian() {
        return menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian));
    }

    private static Map<Boolean, Map<Dish.Type, List<Dish>>> vegetarianDishesByType() {
        return menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.groupingBy(Dish::getType)));
    }

    private static Map<Boolean, Dish> mostCaloricPartitionedByVegetarian() {
        return menu.stream().collect(
                Collectors.partitioningBy(Dish::isVegetarian, Collectors.collectingAndThen(
                        Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)), Optional::get)));
    }


}
